package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileInternalTransferIn;
import com.ruoyi.business.mapper.FileInternalTransferInMapper;
import com.ruoyi.business.service.IFileInternalTransferInPersonService;
import com.ruoyi.business.service.IFileInternalTransferInService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateExtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 内部转入Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class FileInternalTransferInServiceImpl implements IFileInternalTransferInService
{
    @Autowired
    private FileInternalTransferInMapper fileInternalTransferInMapper;
    @Autowired
    private IFileInternalTransferInPersonService fileInternalTransferInPersonService;

    /**
     * 查询内部转入
     *
     * @param id 内部转入ID
     * @return 内部转入
     */
    @Override
    public FileInternalTransferIn selectFileInternalTransferInById(Long id)
    {
        return fileInternalTransferInMapper.selectFileInternalTransferInById(id);
    }

    /**
     * 查询内部转入列表
     *
     * @param fileInternalTransferIn 内部转入
     * @return 内部转入
     */
    @Override
    public List<FileInternalTransferIn> selectFileInternalTransferInList(FileInternalTransferIn fileInternalTransferIn)
    {
        return fileInternalTransferInMapper.selectFileInternalTransferInList(fileInternalTransferIn);
    }

    @Override
    public TableDataInfo<FileInternalTransferIn> selectPageFileInternalTransferInList(FileInternalTransferIn fileInternalTransferIn, PageQuery pageQuery) {
        Page<FileInternalTransferIn> FileInternalTransferInPage = fileInternalTransferInMapper.selectJoinPage(pageQuery.build(), FileInternalTransferIn.class, this.buildQueryWrapper(fileInternalTransferIn));
        return TableDataInfo.build(FileInternalTransferInPage);
    }

    private MPJLambdaWrapper<FileInternalTransferIn> buildQueryWrapper(FileInternalTransferIn fileInternalTransferIn) {
        Map<String, Object> params = fileInternalTransferIn.getParams();
        MPJLambdaWrapper<FileInternalTransferIn> fileInternalTransferInJoinLambdaWrapper = new MPJLambdaWrapper<FileInternalTransferIn>();
        fileInternalTransferInJoinLambdaWrapper.selectAll(FileInternalTransferIn.class)
            .selectAs(SysDept::getDeptName, FileInternalTransferIn::getComingUnitsName)
            .selectAs(SysUser::getNickName,FileInternalTransferIn::getDeliveredPeopleName)
            .selectAs(SysUser::getNickName,FileInternalTransferIn::getRecipientsName)
            .leftJoin(SysDept.class,SysDept :: getDeptId,FileInternalTransferIn :: getComingUnits)
            .leftJoin(SysUser.class,SysUser::getUserId,FileInternalTransferIn :: getDeliveredPeople)
            .leftJoin(SysUser.class,SysUser::getUserId,FileInternalTransferIn :: getRecipients)
            .eq(BeanUtil.isNotEmpty(fileInternalTransferIn.getComingUnits()),FileInternalTransferIn :: getComingUnits,fileInternalTransferIn.getComingUnits())
            .eq(BeanUtil.isNotEmpty(fileInternalTransferIn.getTransmittingType()),FileInternalTransferIn :: getTransmittingType,fileInternalTransferIn.getTransmittingType())
            .eq(BeanUtil.isNotEmpty(fileInternalTransferIn.getTransmittingDate()),FileInternalTransferIn :: getTransmittingDate,fileInternalTransferIn.getTransmittingDate())
            .eq(BeanUtil.isNotEmpty(fileInternalTransferIn.getDeliveredPeople()),FileInternalTransferIn :: getDeliveredPeople,fileInternalTransferIn.getDeliveredPeople())
            .eq(BeanUtil.isNotEmpty(fileInternalTransferIn.getRecipients()),FileInternalTransferIn :: getRecipients,fileInternalTransferIn.getRecipients());

        return fileInternalTransferInJoinLambdaWrapper;
    }

    /**
     * 新增内部转入
     *
     * @param fileInternalTransferIn 内部转入
     * @return 结果
     */
    @Override
    public int insertFileInternalTransferIn(FileInternalTransferIn fileInternalTransferIn)
    {
        fileInternalTransferIn.setCreateTime(DateExtUtils.getNowDate());
        return fileInternalTransferInMapper.insertFileInternalTransferIn(fileInternalTransferIn);
    }

    /**
     * 修改内部转入
     *
     * @param fileInternalTransferIn 内部转入
     * @return 结果
     */
    @Override
    public int updateFileInternalTransferIn(FileInternalTransferIn fileInternalTransferIn)
    {
        fileInternalTransferIn.setUpdateTime(DateExtUtils.getNowDate());
        return fileInternalTransferInMapper.updateFileInternalTransferIn(fileInternalTransferIn);
    }

    /**
     * 批量删除内部转入
     *
     * @param ids 需要删除的内部转入ID
     * @return 结果
     */
    @Override
    public int deleteFileInternalTransferInByIds(Long[] ids)
    {
        for (Long id : ids) {
            fileInternalTransferInPersonService.deleteByInId(id);
        }
        return fileInternalTransferInMapper.deleteFileInternalTransferInByIds(ids);
    }

    /**
     * 删除内部转入信息
     *
     * @param id 内部转入ID
     * @return 结果
     */
    @Override
    public int deleteFileInternalTransferInById(Long id)
    {
        fileInternalTransferInPersonService.deleteByInId(id);
        return fileInternalTransferInMapper.deleteFileInternalTransferInById(id);
    }
}
